﻿using LiveCharts;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Hejian_PCBA_Test.Procdure
{
    public class TesistanceValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double tmp = 0;
            double.TryParse(value.ToString(), out tmp);

            return $"{ConverterTools.UnitConverter(tmp)}Ω";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class VoltageValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double tmp = 0;
            double.TryParse(value.ToString(), out tmp);

            return $"{ConverterTools.UnitConverter(tmp)}V";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToBoolean(value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class IntToPieSegmentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int intValue)
            {
                return new ChartValues<double> { intValue };
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class TestResultConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? result = value as bool?;
            if (parameter.ToString() == "color")
            {
                if (result == true)
                {
                    return Brushes.Green;
                }
                else if (result == false)
                {
                    return Brushes.OrangeRed;
                }
                else
                {
                    return Brushes.Gray;
                }
            }
            else
            {
                if (result == true)
                {
                    return "OK";
                }
                else if (result == false)
                {
                    return "NG";
                }
                else
                {
                    return "Waiting";
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class ResistanceTestResultSingleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Count() != 2)
            {
                return "";
            }
            double lowerLimit = 0;
            double.TryParse(values[0].ToString(), out lowerLimit);
            double upperLimit = 0;
            double.TryParse(values[1].ToString(), out upperLimit);
            if (lowerLimit == -1)
            {
                if (upperLimit == -1)
                {
                    return "无限制";
                }
                else
                {
                    return $"<{ConverterTools.UnitConverter(upperLimit)}Ω";
                }
            }
            else
            {
                if (upperLimit == -1)
                {
                    return $">{ConverterTools.UnitConverter(lowerLimit)}Ω";
                }
                else
                {
                    return $"{ConverterTools.UnitConverter(lowerLimit)}～{ConverterTools.UnitConverter(upperLimit)}Ω";
                }
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class VoltageTestResultSingleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Count() != 2)
            {
                return "";
            }
            double lowerLimit = 0;
            double.TryParse(values[0].ToString(), out lowerLimit);
            double upperLimit = 0;
            double.TryParse(values[1].ToString(), out upperLimit);
            if (lowerLimit == -1)
            {
                if (upperLimit == -1)
                {
                    return "无限制";
                }
                else
                {
                    return $"<{ConverterTools.UnitConverter(upperLimit)}V";
                }
            }
            else
            {
                if (upperLimit == -1)
                {
                    return $">{ConverterTools.UnitConverter(lowerLimit)}V";
                }
                else
                {
                    return $"{ConverterTools.UnitConverter(lowerLimit)}～{ConverterTools.UnitConverter(upperLimit)}V";
                }
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class WidthToColumnsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double width)
            {
                double temp = width / 130;
                int UfGdColumns = (int)temp;
                // 根据窗口宽度返回适当的列数
                return UfGdColumns;
            }

            return 1; // 默认列数
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ConverterTools
    {
        public static string UnitConverter(double value)
        {
            if (value < 1000)
                return value.ToString();
            if (value < 1000000)
                return Math.Round((value / 1000), 2).ToString() + "K";
            if (value < 1000000000)
                return Math.Round((value / 1000000), 2).ToString() + "M";
            else
                return Math.Round((value / 1000000000), 2).ToString() + "G";
        }
    }
}